package com.demo2do.fms.admin.entity.search.supplier;

import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.entity.supplier.common.AuditStatus;
import com.demo2do.fms.entity.supplier.common.Type;

/**
 *
 * @author Ark
 *
 */
public class SupplierAuditSearch implements QueryBuilder {
    
    private String branch;
    
    private AuditStatus[] auditStatus;
    
    private String name;
    
    private String code;
    
    /** 供方类型*/
    private Type type;
    
    private QueryFilter filter;
    
    private QueryOrder order;
    
    /**
     * The default constructor
     */
    public SupplierAuditSearch() {
        order = new QueryOrder().add("lastModifyTime", "DESC");
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if (filter == null || refresh) {
            
            filter = new QueryFilter().addLike("name", name)
                                      .addLike("code", code)
                                      .addEquals("branch.code", branch)
                                      .addEquals("type", type)
                                      .addSentence("status in (:status)", "status", auditStatus);
        }
        
        return filter;
        
    }
    
    /**
     * @param supplierIds
     */
    public void addSupplierIds(List<Long> supplierIds) {
        supplierIds.add(0L);
        this.getPreparedFilter(false).addSentence("id IN (:ids)", "ids", supplierIds);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM Supplier Where disabled = false" + getPreparedFilter(false).clauseSentence() + order.sentence();
        return sentence;
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }
    
    /**
     * 
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }

    /**
     * @return the auditStatus
     */
    public AuditStatus[] getAuditStatus() {
        return auditStatus;
    }

    /**
     * @return the branch
     */
    public String getBranch() {
        return branch;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the type
     */
    public Type getType() {
        return type;
    }

    /**
     * @param auditStatus the auditStatus to set
     */
    public void setAuditStatus(AuditStatus[] auditStatus) {
        this.auditStatus = auditStatus;
    }

    /**
     * @param branch the branch to set
     */
    public void setBranch(String branch) {
        this.branch = branch;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param type the type to set
     */
    public void setType(Type type) {
        this.type = type;
    }

}
